/*
 * CastDRAndroid
 * Copyright (c) 2014 Christian Holm Christensen
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 *
 */
package org.cholm.media.castdr.loaders;

import android.content.Context;
import android.support.v4.content.AsyncTaskLoader;

import org.cholm.media.castdr.backend.LiveChannel;

import org.cholm.media.castdr.LogUtils; import static org.cholm.media.castdr.LogUtils.LOGd;
/**
 * @author cholm
 */
public class LiveLoader extends AsyncTaskLoader<LiveChannel> {
    private static final String TAG = LogUtils.makeLogTag(LiveLoader.class);
    private LiveChannel mCh = null;

    public LiveLoader(Context context, LiveChannel sub) {
        super(context);
        mCh = sub;
        LOGd(TAG, "Created loader");
    }

    @Override
    public LiveChannel loadInBackground() {
        LOGd(TAG, "Loading in background from " + mCh.getUrl());
        LiveChannel ret = mCh;
        if (!mCh.load()) {
            ret = null;
        }

        return ret;
    }

    @Override
    protected void onStartLoading() {
        LOGd(TAG, "Starting load");
        super.onStartLoading();
        forceLoad();
    }

    @Override
    protected void onReset() {
        LOGd(TAG, "Clearing data");
    }

    @Override
    protected void onForceLoad() {
        LOGd(TAG, "Force loading");
        // mData.clear();
        super.onForceLoad();
    }

    /**
     * Handles a request to stop the Loader.
     */
    @Override
    protected void onStopLoading() {
        // Attempt to cancel the current load task if possible.
        LOGd(TAG, "Stopping load");
        cancelLoad();
    }
}
